/**
    Die Ewigen Extension
    Copyright (C) 2011 DonKrieg @ sonkyu@googlemail.com

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

// Jagdbootsupport
// fleetpoints for every fleet
// sum of all fleetpoints
$(
        '<tr>'
        +'<td class="cc">Jagdbootsupport</td>'
        +'<td class="cc"></td>'
        +'<td class="cc" id="boat_1">0<br/>(0)</td>'
        +'<td class="cc" id="boat_2">0<br/>(0)</td>'
        +'<td class="cc" id="boat_3">0<br/>(0)</td>'
        +'</tr>'
        
        +'<tr>'
        +'<td class="cc">Flottenpunkte</td>'
        +'<td class="cc" ></td>'
        +'<td class="cc" id="points_1">0</td>'
        +'<td class="cc" id="points_2">0</td>'
        +'<td class="cc" id="points_3">0</td>'
        +'</tr>'
        
        +'<tr>'
        +'<td class="cc">Gesamt</td>'
        +'<td class="cc"></td>'
        +'<td class="cc" colspan="3" id="points_total">0</td>'
        +'</tr>'
).insertBefore('tr:contains("Angriffsformation"):last');


(function(jQuery) {

    $.ships = new Array();
    
    $.fn.shipnames = function() {
        $.ships["name"]                 = new Array();
        var iterator    = 0;
        
        this.each(function() {
            $.ships["name"][iterator]   = $(this).text().trim();
            iterator++;
        });
    }

    $.fn.shipcalc = function() {
        var iterator    = 0;

        this.each(function() {
            if($.ships[iterator%3] == undefined) {
                $.ships[iterator%3] = new Array();
            }

            // we have a "moving" fleet"
            if($(this).children().val() == 0) {
                $.ships[iterator%3][parseInt(iterator/3)] = $.ewigen.util.pointint2int($(this).text());
            }
            // fleet is still at home
            else {
                $.ships[iterator%3][parseInt(iterator/3)] = $.ewigen.util.pointint2int($(this).children().val());
            }

            iterator++;
        });
    }

    /**
     * calculate the estimated boat support for the given fleet
     *
     * @param   idx             int     id of the fleet to calc (0 = home, 1 = fleet1, ...)
     * @param   support         array   contains an array of ints, tracking which ship can transport which
     */
    $.fn.boatSupport = function( idx, support ) {
        //TODO: move to config file
        // 500 boats are the base support
        var num = 500;

        for(var i=0; i<$.ships[idx].length; i++) {
            //TODO: check if the current is really a boat
            num += parseInt($.ships[idx][i] * support[i]);
        }

        this.html($.ewigen.util.int2pointint($.ships[idx][1]) + "<br/>(" + $.ewigen.util.int2pointint(num) + ")");
    }
    
    $.fn.fillPoints = function( idx, ships ) {
        var num = 0;

        for(var i=0; i<$.ships[idx].length; i++) {
            num += parseInt($.ships[idx][i] * ships[i]);
        }

        this.text($.ewigen.util.int2pointint(num));
    }

    $.fn.totalPoints = function() {
        var num = 0;
        num += parseInt($.ewigen.util.pointint2int($("#points_1").text()));
        num += parseInt($.ewigen.util.pointint2int($("#points_2").text()));
        num += parseInt($.ewigen.util.pointint2int($("#points_3").text()));
        this.text($.ewigen.util.int2pointint(num));
    }

    $.parseShipArray = function() {
        // for every fleet
        for(var i=0; i<3; i++) {
            var newShips = new Array();

            // set ships to zero per default
            for(var j=0; j<$.ewigen.config.racedata[localStorage['sde_race_short']]["SHIPS"].length; j++) {
                newShips[j] = 0;
            }

            // for every ship we are allowed to build
            for(var k=0; k<$.ships[i].length; k++) {
                newShips[$.ewigen.getShipID(localStorage['sde_race_short'], $.ships["name"][k])] = $.ships[i][k];
            }

            $.ships[i] = newShips;
        }
    }
    
})(jQuery);


/**
 * this method calls all necessary methods to do the calculation needed
 */
calcFleet = function() {
    // get all the td's containing the ship numbers
    $('td.cc:has("input[name^=m]")').shipcalc();

    $.parseShipArray();

    // calc the fleets supported boats
    $("#boat_1").boatSupport(0, [0,0,0,10,20,0,0,40]);
    $("#boat_2").boatSupport(1, [0,0,0,10,20,0,0,40]);
    $("#boat_3").boatSupport(2, [0,0,0,10,20,0,0,40]);

    // calc the fleets points
    $("#points_1").fillPoints(0, $.fn.getShipData(localStorage['sde_race_short'], $.ewigen.config.SHIP_POINTS));
    $("#points_2").fillPoints(1, $.fn.getShipData(localStorage['sde_race_short'], $.ewigen.config.SHIP_POINTS));
    $("#points_3").fillPoints(2, $.fn.getShipData(localStorage['sde_race_short'], $.ewigen.config.SHIP_POINTS));

    $("#points_total").totalPoints();
}

// get the td's containing the ship names
$('td.cl').shipnames();

// first calc after pageload
calcFleet();

// every time the user changes the fleet we need to calc the points and support again
$('td.cc:has("input[name^=m]")').keyup(function() {
    calcFleet();
});
